package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project.entity.ProjectMember;

/**
 * 项目成员Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-27
 */
@Mapper
public interface ProjectMemberMapper {

	/**
	 * 查询项目成员
	 * 
	 * @param id 项目成员主键
	 * @return 项目成员
	 */
	@Select("select pm.id, pm.user_id, pm.project_id, pm.role_id, pm.create_time, u.user_name as user_name, "
			+ "pr.name as role_name, p.name as project_name, pm.create_by, pm.update_by, pm.update_time, pm.remark, "
			+ "u.avatar as avatar "
			+ "from t_project_member pm "
			+ "left join t_user u on pm.user_id = u.user_id "
			+ "left join t_project_role pr on pm.role_id = pr.id "
			+ "left join t_project p on pm.project_id = p.id "
			+ "where pm.id = #{id}")
	public ProjectMember selectProjectMemberById(Long id);

	/**
	 * 查询项目成员列表
	 * 
	 * @param projectMember 项目成员
	 * @return 项目成员集合
	 */
	@Select("<script>"
			+ "select pm.id, pm.user_id, pm.project_id, pm.role_id, pm.create_time, u.user_name as user_name, "
			+ "pr.name as role_name, p.name as project_name, pm.create_by, pm.update_by, pm.update_time, pm.remark, "
			+ "u.avatar as avatar "
			+ "from t_project_member pm "
			+ "left join t_user u on pm.user_id = u.user_id "
			+ "left join t_project_role pr on pm.role_id = pr.id "
			+ "left join t_project p on pm.project_id = p.id "
			+ "where 1=1"
			+ "<if test='projectMember.userId != null'> and pm.user_id = #{projectMember.userId}</if>"
			+ "<if test='projectMember.projectId != null'> and pm.project_id = #{projectMember.projectId}</if>"
			+ "<if test='projectMember.roleId != null'> and pm.role_id = #{projectMember.roleId}</if>"
			+ "<if test='projectMember.userName != null and projectMember.userName != \"\"'> and u.user_name like concat('%', #{projectMember.userName}, '%')</if>"
			+ "<if test='projectMember.roleName != null and projectMember.roleName != \"\"'> and pr.name like concat('%', #{projectMember.roleName}, '%')</if>"
			+ "<if test='projectMember.projectName != null and projectMember.projectName != \"\"'> and p.name like concat('%', #{projectMember.projectName}, '%')</if>"
			+ "</script>")
	public List<ProjectMember> selectProjectMemberList(@Param("projectMember") ProjectMember projectMember);

	/**
	 * 根据项目ID查询项目成员列表
	 * 
	 * @param projectId 项目ID
	 * @return 项目成员集合
	 */
	@Select("select pm.id, pm.user_id, pm.project_id, pm.role_id, pm.create_time, u.user_name as user_name, "
			+ "pr.name as role_name, pr.sort_order as sort_order, p.name as project_name, pm.create_by, pm.update_by, pm.update_time, pm.remark, "
			+ "u.avatar as avatar "
			+ "from t_project_member pm "
			+ "left join t_user u on pm.user_id = u.user_id "
			+ "left join t_project_role pr on pm.role_id = pr.id "
			+ "left join t_project p on pm.project_id = p.id "
			+ "where pm.project_id = #{projectId} "
			+ "order by pr.sort_order asc, pm.create_time asc")
	public List<ProjectMember> selectProjectMemberListByProjectId(Long projectId);

	/**
	 * 新增项目成员
	 * 
	 * @param projectMember 项目成员
	 * @return 结果
	 */
	@Insert("<script>" + "insert into t_project_member" + "<trim prefix='(' suffix=')' suffixOverrides=','>"
            + "<if test='userId != null'>user_id,</if>"
            + "<if test='projectId != null'>project_id,</if>"
            + "<if test='roleId != null'>role_id,</if>"
            + "create_time,"
            + "<if test='createBy != null'>create_by,</if>"
            + "<if test='updateBy != null'>update_by,</if>"
            + "<if test='updateTime != null'>update_time,</if>"
            + "<if test='remark != null'>remark,</if>"
            + "</trim>"
			+ "<trim prefix='values (' suffix=')' suffixOverrides=','>"
            + "<if test='userId != null'>#{userId},</if>"
            + "<if test='projectId != null'>#{projectId},</if>"
            + "<if test='roleId != null'>#{roleId},</if>"
            + "now(),"
            + "<if test='createBy != null'>#{createBy},</if>"
            + "<if test='updateBy != null'>#{updateBy},</if>"
            + "<if test='updateTime != null'>#{updateTime},</if>"
            + "<if test='remark != null'>#{remark},</if>"
			+ "</trim>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertProjectMember(ProjectMember projectMember);

	/**
	 * 修改项目成员
	 * 
	 * @param projectMember 项目成员
	 * @return 结果
	 */
	@Update("<script>" + "update t_project_member" + "<trim prefix='SET' suffixOverrides=','>"
			+ "<if test='userId != null'>user_id = #{userId},</if>"
			+ "<if test='projectId != null'>project_id = #{projectId},</if>"
			+ "<if test='roleId != null'>role_id = #{roleId},</if>"
			+ "<if test='updateBy != null'>update_by = #{updateBy},</if>"
			+ "update_time = now(),"
			+ "<if test='remark != null'>remark = #{remark},</if>" + "</trim>" 
			+ "where id = #{id}"
			+ "</script>")
	public int updateProjectMember(ProjectMember projectMember);

	/**
	 * 删除项目成员
	 * 
	 * @param id 项目成员主键
	 * @return 结果
	 */
	@Update("delete from t_project_member where id = #{id}")
	public int deleteProjectMemberById(Long id);

	/**
	 * 批量删除项目成员
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Update("<script>" + "delete from t_project_member where id in "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	public int deleteProjectMemberByIds(String[] ids);

	/**
	 * 检查用户是否已在项目中
	 * 
	 * @param userId 用户ID
	 * @param projectId 项目ID
	 * @return 存在数量
	 */
	@Select("select count(1) from t_project_member where user_id = #{userId} and project_id = #{projectId}")
	public int checkUserInProject(Long userId, Long projectId);

	/**
	 * 检查用户是否已在项目中（排除当前成员）
	 * 
	 * @param userId 用户ID
	 * @param projectId 项目ID
	 * @param excludeMemberId 排除的成员ID
	 * @return 存在数量
	 */
	@Select("select count(1) from t_project_member where user_id = #{userId} and project_id = #{projectId} and id != #{excludeMemberId}")
	public int checkUserInProjectExcludeCurrent(@Param("userId") Long userId, @Param("projectId") Long projectId, @Param("excludeMemberId") Long excludeMemberId);

	/**
	 * 根据用户ID查询项目成员列表
	 * 
	 * @param userId 用户ID
	 * @return 项目成员集合
	 */
	@Select("select pm.id, pm.user_id, pm.project_id, pm.role_id, pm.create_time, u.user_name as user_name, "
			+ "pr.name as role_name, p.name as project_name, pm.create_by, pm.update_by, pm.update_time, pm.remark, "
			+ "u.avatar as avatar "
			+ "from t_project_member pm "
			+ "left join t_user u on pm.user_id = u.user_id "
			+ "left join t_project_role pr on pm.role_id = pr.id "
			+ "left join t_project p on pm.project_id = p.id "
			+ "where pm.user_id = #{userId}")
	public List<ProjectMember> selectProjectMemberListByUserId(Long userId);

	/**
	 * 统计项目成员数量
	 * 
	 * @param projectId 项目ID
	 * @return 成员数量
	 */
	@Select("select count(*) from t_project_member where project_id = #{projectId}")
	Integer selectCountByProjectId(Long projectId);
}